import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicketSeller implements Runnable {
    private String name;
    private static int totalTickets = 100;
    private static Lock lock = new ReentrantLock();

    public TicketSeller(String name) {
        this.name = name;
    }

    public void run() {
        while (totalTickets > 0) {
            try {
                lock.lock();
                if (totalTickets > 0) {
                    System.out.println(name + "卖出了第" + totalTickets + "张票。");
                    totalTickets--;
                } else {
                    System.out.println("票已售完。");
                    break;
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        TicketSeller seller1 = new TicketSeller("窗口1");
        TicketSeller seller2 = new TicketSeller("窗口2");
        TicketSeller seller3 = new TicketSeller("窗口3");

        Thread thread1 = new Thread(seller1);
        Thread thread2 = new Thread(seller2);
        Thread thread3 = new Thread(seller3);

        thread1.start();
        thread2.start();
        thread3.start();

        try {
            thread1.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
